package com.five.admin.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.five.admin.dto.BannerDto;
import com.five.admin.mapper.BannerMapper;
import com.five.admin.server.BannerService;
import com.five.pojo.entity.Banner;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {
    @Override
    public Page<Banner> findAll(Page<Banner> page, BannerDto bannerDto) {
        Page<Banner> resultPage = getBaseMapper().findAll(page, bannerDto);
        return resultPage;
    }

    @Override
    public List<Banner> findBannerByPosition(Integer position) {
        System.err.println(position);
        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("position", position);
        List<Banner> banners = getBaseMapper().selectList(queryWrapper);
        List<Banner> resultBanners = new ArrayList<>();
        LocalDateTime currentTime = LocalDateTime.now();
        //如果banner不为空，则进行时间判断
        if (!banners.isEmpty()) {
            banners.forEach(banner -> {
                LocalDateTime endTime = banner.getEndTime();
                LocalDateTime startTime = banner.getStartTime();
                if (currentTime.isAfter(startTime) && currentTime.isBefore(endTime)) {
                    resultBanners.add(banner);
                }
            });
        }
        return resultBanners;
    }
}
